import logging

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect
from portal.model.meta import Session, Base
from portal.lib.base import BaseController, render
from portal.lib.auth import check_if_user_is_already_connected, \
                            verify_user_login, \
                            user_logout
from portal.model import User                            
log = logging.getLogger(__name__)

class AuthController(BaseController):

    def index(self):
        if check_if_user_is_already_connected():
            return redirect('/dashboard')
        return redirect('/auth/login')


    def login(self):
        return render('/auth/login.mako')


    def logout(self):
        user_logout()
        if 'user' not in session:
            return redirect('/')


    def submit(self):
        login = request.params.get('email')
        passwd = request.params.get('password')
        
        return verify_user_login(login, passwd)
